package com.ksd.modules.special.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ksd.common.enums.DeletedEnums;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.modules.special.entity.AdminSpecialBind;
import com.ksd.modules.special.mapper.AdminSpecialBindMapper;
import com.ksd.modules.special.po.AdminSpecialBindSavePO;
import com.ksd.modules.special.service.IAdminSpecialBindDetailService;
import com.ksd.modules.special.service.IAdminSpecialBindService;
import com.ksd.modules.special.vo.AdminSpecialBindDetailVo;
import com.ksd.modules.special.vo.AdminSpecialBindVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class AdminSpecialBindServiceImpl extends BaseServiceImpl<AdminSpecialBindMapper, AdminSpecialBind> implements IAdminSpecialBindService {


    @Autowired
    private IAdminSpecialBindDetailService adminSpecialBindDetailService;

    @Override
    public List<AdminSpecialBindVo> findAdminSpecialBinds(Long specialId) {
        LambdaQueryWrapper<AdminSpecialBind> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminSpecialBind::getSpecialId,specialId);
        lambdaQueryWrapper.eq(AdminSpecialBind::getSystemId, SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(AdminSpecialBind::getIsDeleted, DeletedEnums.UN_DELETED.getCode());
        lambdaQueryWrapper.orderByAsc(AdminSpecialBind::getSorted);
        List<AdminSpecialBindVo> adminSpecialBindVos = tranferListBo(this.list(lambdaQueryWrapper),AdminSpecialBindVo.class);
        if(CollectionUtils.isNotEmpty(adminSpecialBindVos)){
            for (AdminSpecialBindVo adminSpecialBindVo : adminSpecialBindVos) {
                List<AdminSpecialBindDetailVo> adminSpecialBindDetails = adminSpecialBindDetailService
                        .findAdminSpecialBindDetails(specialId, adminSpecialBindVo.getId(), adminSpecialBindVo.getType());
                adminSpecialBindVo.setChildren(adminSpecialBindDetails);
            }
            return adminSpecialBindVos;
        }
        return new ArrayList<>();
    }

    @Override
    public boolean saveUpdateAdminSpecialBind(AdminSpecialBindSavePO adminSpecialBindSavePO) {
        AdminSpecialBind adminSpecialBind = tranferBo(adminSpecialBindSavePO, AdminSpecialBind.class);
        adminSpecialBind.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminSpecialBind.setSystemName(SystemIdThrealLocal.get().getSystemName());
        return this.saveOrUpdate(adminSpecialBind);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeAdminSpecialBindById(Long id) {
        boolean b = this.removeById(id);
        if(b){
            adminSpecialBindDetailService.removeAdminSpecialBindDetailByBindId(id);
        }
        return b;
    }

    @Override
    public AdminSpecialBindVo getAdminSpecialBindById(Long id) {
        return tranferBo( this.getById(id),AdminSpecialBindVo.class);
    }
    /**
     * 复制
     * @param opid
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean copyData(Long opid){
        AdminSpecialBind data = this.getById(opid);
        if(Objects.nonNull(data)){
            data.setId(null);
            data.setStatus(0);
            data.setCreateTime(LocalDateTime.now());
            data.setUpdateTime(LocalDateTime.now());
            data.setTitle(data.getTitle()+"[副本]");
            this.save(data);
        }
        return true;
    }

}